var {
    get,
    post
} = require('../../comm/localHttp');
var {
    formatDateToString,
    dataAsYMDHMS,
    dataAsYMDHM
} = require('./../../comm/commHelper');
// 页面的js文件
Component({
    data: {
        page: 1,
        limit: 5,
        totalCount: 0,
        buttonLeft: 20,
        buttonTop: 20,
        touchStartPosition: {
            x: 0,
            y: 0
        },
        listLessonInfo: [],
    },
    methods: {
        // 按钮拖动开始事件
        buttonMove: function (e) {
            const touchStartPosition = this.data.touchStartPosition;
            const buttonLeft = touchStartPosition.x + e.touches[0].clientX - touchStartPosition.clientX;
            const buttonTop = touchStartPosition.y + e.touches[0].clientY - touchStartPosition.clientY;
            this.setData({
                buttonLeft,
                buttonTop
            });
        },

        // 按钮拖动结束事件
        buttonEnd: function (e) {
            this.setData({
                touchStartPosition: {
                    x: this.data.buttonLeft,
                    y: this.data.buttonTop,
                    clientX: e.changedTouches[0].clientX,
                    clientY: e.changedTouches[0].clientY
                }
            });
        },

        // 扫码按钮点击事件
        scanCode: function () {
            wx.scanCode({
                success: async function (res_scan) {
                    const result = res_scan.result; // 获取扫码结果
                    // 进行下一步操作
                    console.log('scan res', res_scan);
                    const res = await get('/training/lesson/id', {
                        trainingId: result
                    });

                    console.log(res);
                    const trainingName = res.data.trainingName;
                    const userInfo = wx.getStorageSync('userInfo');
                    const res2 = await get('/training/course/sign', {
                        trainingId: result,
                        userId: userInfo.userId,
                    });
                    console.log(res2);
                    if (res2 == null) {
                        wx.showModal({
                            title: '提示',
                            content: '获取用户是否签到异常!'
                        });
                        return;
                    }
                    if (res2.code != 0) {
                        wx.showModal({
                            title: '提示',
                            content: '获取用户是否签到异常 ' + res2.msg
                        });
                        return;
                    }
                    const isSign = res2.data;
                    console.log('isSign', isSign);
                    if (isSign == 1) {
                        wx.showModal({
                            title: '提示',
                            content: '当前培训您已经签过到,不需要再签到!',
                            showCancel: false
                        });
                        return;
                    }

                    wx.showModal({
                        title: '提示',
                        content: '课程为:' + trainingName + '  是否签到?',
                        success: async function (res) {
                            if (res.confirm) {
                                // 进行下一步操作
                                console.log('scan res', res_scan);
                                var currenttimemillis = (new Date().getTime() - new Date(1970, 0, 1, 0, 0, 0, 0).getTime());
                                const res_sign = await post('/training/course', {
                                    trainingId: result,
                                    userId: userInfo.userId,
                                    signStartTime: currenttimemillis,

                                });
                                if (res_sign == null) {
                                    wx.showModal({
                                        title: '提示',
                                        content: '签到异常!'
                                    });
                                    return;
                                }
                                if (res_sign.code != 0) {
                                    wx.showModal({
                                        title: '提示',
                                        content: '签到失败 ' + res_sign.msg
                                    });
                                    return;
                                }
                                wx.showModal({
                                    title: '提示',
                                    content: '签到成功!!'
                                });
                                //这里更新数据
                                this.loadData();
                                return;
                            } else if (res.cancel) {
                                //点击取消按钮
                            }
                        }
                    });
                    return;

                }
            })
        },
        pagerChanged: function (page) {
            console.log('pagerChanged', page);
            this.loadData();
        },
        loadData: async function () {
            const page = this.data.page;
            const limit = this.data.limit;
            var questionnaires = [];
            const userInfo = wx.getStorageSync('userInfo');
            const res = await get('/training/course/id', {
                id: userInfo.userId,
                page,
                limit
            });
            if (res.code == 0) {
                const listLessonInfo = res.data.records;
                const total = res.data.total;
                listLessonInfo.forEach(element => {
                    element.signStartTimeStr = dataAsYMDHMS(new Date(element.signStartTime));
                    element.startTimeStr = dataAsYMDHM(new Date(element.startTime));
                    element.endTimeStr = dataAsYMDHM(new Date(element.endTime));
                });


                this.setData({
                    listLessonInfo: listLessonInfo,
                    totalCount: total
                });
                console.log('获取到的数据：', listLessonInfo);
            }
        }
    },

    ready: function () {
        // 组件布局完成后执行的操作
        // 例如发起数据请求、DOM操作等
        // ...
        this.setData({
            totalCount: 0,
            page: 1,
        });
        this.loadData();
    }
});